package com.sendi.mobilework;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.sendi.mobilework.global.GlobalContent;

public class NewsDetailActivity extends AppCompatActivity {

    private WebView mWebView;
    private ProgressBar mProgressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //初始化布局
        initView();

        //初始化WebView的设置
        initWebSetting();


        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                //开始加载
                mProgressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //加载结束
                mProgressBar.setVisibility(View.INVISIBLE);
            }

            /*
            * url：表示要跳转的url
            * */
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        int id = getIntent().getIntExtra("id", 0);
        String url = GlobalContent.URL + id;
        Log.i("URL", "onCreate: " + url);
        mWebView.loadUrl(url);
    }

    /**
     * 初始化布局
     */
    private void initView() {
        setContentView(R.layout.activity_news_detail);
        mWebView = (WebView) findViewById(R.id.webView);
        mProgressBar = (ProgressBar) findViewById(R.id.pb_web);
    }

    /**
     * 初始化WebView的设置
     */
    public void initWebSetting(){
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);//能够执行js代码
        settings.setBuiltInZoomControls(true);//显示缩小放大按钮
        settings.setUseWideViewPort(true);//支持双击放大缩小
    }

    /**
     * 返回
     * @param view
     */
    public void back(View view) {
        this.finish();
    }
}
